package com.quectel.core.module.alarm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.core.module.alarm.dao.AlarmRuleConditionDao;
import com.quectel.core.module.alarm.dto.AlarmRuleConditionDto;
import com.quectel.core.module.alarm.entity.AlarmRuleConditionEntity;
import com.quectel.core.module.alarm.service.AlarmRuleConditionService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:23:34
 */
@DubboService
public class AlarmRuleConditionServiceImpl implements AlarmRuleConditionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AlarmRuleConditionServiceImpl.class);

    @Autowired
    private AlarmRuleConditionDao alarmConditionDao;

    @Override
    public List<AlarmRuleConditionDto> selectByRuleId(Long ruleId) {
        List<AlarmRuleConditionEntity> entityList = alarmConditionDao.selectList(new QueryWrapper<AlarmRuleConditionEntity>().eq("rule_id", ruleId));
        return CopyUtils.copyList(entityList, AlarmRuleConditionDto.class);
    }

    @Override
    public List<AlarmRuleConditionDto> queryList(Map<String, Object> params) {
        List<AlarmRuleConditionEntity> list = alarmConditionDao.queryList(params);
        return CopyUtils.copyList(list, AlarmRuleConditionDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return alarmConditionDao.queryTotal(params);
    }

    @Override
    public Long save(AlarmRuleConditionDto dto) {
        AlarmRuleConditionEntity entity = CopyUtils.copyObj(dto, AlarmRuleConditionEntity.class);
        entity.setId(Snowflake.nextId());
        alarmConditionDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AlarmRuleConditionDto dto) {
        AlarmRuleConditionEntity entity = CopyUtils.copyObj(dto, AlarmRuleConditionEntity.class);
        alarmConditionDao.updateById(entity);
    }

    @Override
    public AlarmRuleConditionDto selectById(Long id) {
        AlarmRuleConditionEntity entity = alarmConditionDao.selectById(id);
        AlarmRuleConditionDto alarmRuleConditionDto = CopyUtils.copyObj(entity, AlarmRuleConditionDto.class);
        return alarmRuleConditionDto;
    }

    @Override
    public void deleteById(Long id) {
        alarmConditionDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
